package com.kitbox.hl.meshow.common.aop;


import com.kitbox.hl.meshow.common.annotation.RequestCheckAnnotation;
import com.kitbox.hl.meshow.common.web.RequestCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
public class RequestAspect {

	private static final Log logger = LogFactory.getLog(RequestAspect.class);

	@Around(value = "@annotation(requestCheckAnnotation)")
	public Object logTime(ProceedingJoinPoint pjd, RequestCheckAnnotation requestCheckAnnotation) throws Throwable {
		String signature = pjd.getSignature().toString();
		logger.info("待执行方法" + signature + "参数为" + Arrays.toString(pjd.getArgs()));
		for (Object o : pjd.getArgs()) {
			if (o instanceof HttpServletRequest){
				if (RequestCheck.repeatRequestTime((HttpServletRequest) o)<5000){
					if (RequestCheck.sameRequestCheck((HttpServletRequest) o)){
						return "";
					}
				}
			}
		}
		return pjd.proceed();
	}
}
